"""
病历相关的 Pydantic 模型
"""
from typing import Optional, Any
from datetime import datetime
from uuid import UUID
from pydantic import BaseModel, Field, ConfigDict


# ========== 用药信息 ==========
class MedicationBase(BaseModel):
    """用药基础模型"""
    medication_name: str = Field(description="药品名称")
    category: Optional[str] = Field(default=None, description="药品分类")
    specification: Optional[str] = Field(default=None, description="规格")
    route: Optional[str] = Field(default=None, description="给药途径")
    frequency: Optional[str] = Field(default=None, description="频次")
    dosage_value: Optional[int] = Field(default=None, description="剂量数值")
    dosage_unit: Optional[str] = Field(default=None, description="剂量单位")
    duration: Optional[int] = Field(default=None, description="用药天数")
    total_quantity: Optional[str] = Field(default=None, description="总量")


class MedicationCreate(MedicationBase):
    """创建用药"""
    pass


class MedicationResponse(MedicationBase):
    """用药响应"""
    id: UUID
    record_id: UUID
    created_at: datetime
    
    model_config = ConfigDict(from_attributes=True)


# ========== 医嘱信息 ==========
class InstructionBase(BaseModel):
    """医嘱基础模型"""
    instruction_text: str = Field(description="医嘱内容")
    instruction_type: Optional[str] = Field(default=None, description="医嘱类型")


class InstructionCreate(InstructionBase):
    """创建医嘱"""
    pass


class InstructionResponse(InstructionBase):
    """医嘱响应"""
    id: UUID
    record_id: UUID
    created_at: datetime
    
    model_config = ConfigDict(from_attributes=True)


# ========== 病历信息 ==========
class MedicalRecordBase(BaseModel):
    """病历基础模型"""
    hospital_name: Optional[str] = Field(default=None, description="医院名称")
    record_type: str = Field(default="门诊病历", description="病历类型")
    patient_name: str = Field(description="患者姓名")
    patient_age: Optional[int] = Field(default=None, description="患者年龄")
    patient_gender: Optional[str] = Field(default=None, description="患者性别")
    patient_card_number: Optional[str] = Field(default=None, description="门诊卡号")
    patient_phone: Optional[str] = Field(default=None, description="联系电话")
    patient_address: Optional[str] = Field(default=None, description="现住址")
    department: Optional[str] = Field(default=None, description="科室")
    visit_date: Optional[datetime] = Field(default=None, description="就诊日期")
    chief_complaint: Optional[str] = Field(default=None, description="主诉")
    present_illness: Optional[str] = Field(default=None, description="现病史")
    allergy_history: Optional[str] = Field(default=None, description="过敏史")
    past_history: Optional[str] = Field(default=None, description="既往史")
    personal_history: Optional[str] = Field(default=None, description="个人史")
    menstrual_history: Optional[str] = Field(default=None, description="月经史")
    physical_exam: Optional[str] = Field(default=None, description="体格检查")
    auxiliary_exam: Optional[str] = Field(default=None, description="辅助检查")
    diagnosis: Optional[list[str]] = Field(default=None, description="诊断列表")
    doctor_name: Optional[str] = Field(default=None, description="医生姓名")


class MedicalRecordCreate(MedicalRecordBase):
    """创建病历"""
    raw_data: dict[str, Any] = Field(description="原始JSON数据")
    uploaded_by: Optional[str] = Field(default=None, description="上传者")


class MedicalRecordUpdate(BaseModel):
    """更新病历"""
    hospital_name: Optional[str] = None
    record_type: Optional[str] = None
    patient_name: Optional[str] = None
    patient_age: Optional[int] = None
    patient_gender: Optional[str] = None
    department: Optional[str] = None
    chief_complaint: Optional[str] = None
    present_illness: Optional[str] = None
    diagnosis: Optional[list[str]] = None


class MedicalRecordResponse(MedicalRecordBase):
    """病历响应（基础信息）"""
    id: UUID
    created_at: datetime
    updated_at: datetime
    
    model_config = ConfigDict(from_attributes=True)


class MedicalRecordDetailResponse(MedicalRecordResponse):
    """病历详细响应（包含关联数据）"""
    medications: list[MedicationResponse] = Field(default_factory=list)
    instructions: list[InstructionResponse] = Field(default_factory=list)
    raw_data: dict[str, Any] = Field(description="原始JSON数据")


class MedicalRecordListItem(BaseModel):
    """病历列表项"""
    id: UUID
    patient_name: str
    patient_gender: Optional[str]
    patient_age: Optional[int]
    department: Optional[str]
    visit_date: Optional[datetime]
    diagnosis: Optional[list[str]]
    created_at: datetime
    has_assessment: bool = Field(default=False, description="是否已评估")
    
    model_config = ConfigDict(from_attributes=True)


# ========== 病历上传 ==========
class RecordUploadRequest(BaseModel):
    """病历上传请求"""
    uploaded_by: Optional[str] = Field(default="匿名用户", description="上传者")


class RecordUploadResponse(BaseModel):
    """病历上传响应"""
    success_count: int = Field(description="成功数量")
    failed_count: int = Field(description="失败数量")
    records: list[MedicalRecordResponse] = Field(description="上传成功的病历")
    errors: list[dict[str, str]] = Field(default_factory=list, description="错误信息")


# ========== 查询参数 ==========
class RecordQueryParams(BaseModel):
    """病历查询参数"""
    patient_name: Optional[str] = Field(default=None, description="患者姓名")
    department: Optional[str] = Field(default=None, description="科室")
    start_date: Optional[datetime] = Field(default=None, description="开始日期")
    end_date: Optional[datetime] = Field(default=None, description="结束日期")
    has_assessment: Optional[bool] = Field(default=None, description="是否已评估")

